package dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import dao.StoreDao;
import vo.Commodity;
import vo.CommodityItem;
import vo.OrderForm;
import vo.Store;
@Repository("sd")
public class StoreDaoImpl implements StoreDao {
	@PersistenceContext(name="un")
	private EntityManager em;
	private String jpql;
	
	@Override
	public int addStu(String email,String passwd) {
		int rs=0;
		jpql="update Store s set s.passwd=:passwd where s.email=:email";
		//em.persist(s);
		rs = em.createQuery(jpql)
				.setParameter("passwd", passwd)
				.setParameter("email", email)
				.executeUpdate();
		return rs;
	}

	@Override
	public Store findStu(String email,String passwd) {
		System.out.println("this is dao:" + email + passwd);
		String jpql="select s from Store s where s.email=:email and s.passwd=:passwd";
		List<Store> ls=em.createQuery(jpql)
				.setParameter("email", email)
				.setParameter("passwd", passwd)
				.getResultList();
		if(ls.isEmpty()) return null;
		else return ls.get(0);
	}

	@Override
	public int post(Store s) {
		//int rs=0;
		em.persist(s);
		System.out.println("s.id"+s.getStoreId());
		return s.getStoreId();
	}

	@Override
	public Store getInfoByEmail(String email, Store s) {
		System.out.println("this is daoimp : " + email);
		jpql="select s from Store s where s.email=:email";
		List<Store> ls=em.createQuery(jpql)
				.setParameter("email", email)
				.getResultList();
		if (ls.size()>0) {
			return ls.get(0);
		}
		System.out.println("this is dao:" + s.toString());
		return null;
	}

	@Override
	public int updateStatus(String email, int status) {
		int rs=0;
		jpql="update Store s set s.status=:status where s.email=:email";
		//em.persist(s);
		rs = em.createQuery(jpql)
				.setParameter("status", status)
				.setParameter("email", email)
				.executeUpdate();
		return rs;

	}

	@Override
	public List<Commodity> findFood(String email) {
		
		System.out.println(email);
		jpql="select s.storeId from Store s where s.email=:email";
		int id = (int)em.createQuery(jpql)
				.setParameter("email", email)
				.getSingleResult();
		
		jpql = "select s from Commodity s where s.storeId=:id";
		List<Commodity> ls=em.createQuery(jpql)
				.setParameter("id", id)
				.getResultList();

		return ls;
	}

	@Override
	public int insertFood(Commodity commodity) {
		em.persist(commodity);
		return commodity.getCommodityId();
	}
	
	

	@Override
	public Commodity findFoodById(int id) {
		jpql = "select c from commodity c where c.commodityId =:id";
		List<Commodity> ls=em.createQuery(jpql)
				.setParameter("id", id)
				.getResultList();
		if (ls!=null) {
			
			return ls.get(0);
		}else {
			return null;
		}
	}

	@Override
	public int deleteFood(Commodity commodity) {
		
		em.remove(commodity);
		return 1;
	}
	
	//鏌ヨ璁㈠崟
		@Override
		public List<OrderForm> findStoreOrderForms(String storeId) {
			
			jpql = "select s.storeId from Store s where s.email=:storeId";
			int id = (Integer)em.createQuery(jpql).setParameter("storeId", storeId).getResultList().get(0);
			System.out.println("鎸佷箙鍖栧眰  storeid涓�" + storeId);

			jpql = "select o from OrderForm o where o.storeId=:storeId and o.orderStatus=:orderStatus";

			List<OrderForm> orders = em.createQuery(jpql)
					.setParameter("orderStatus", 1)
					.setParameter("storeId", id).getResultList();

			return orders;
		}
		//dao 灞傛煡璇㈡潯鐩�
		@Override
		public List<CommodityItem> findItemsByOrderId(String orderNumber) {
			System.out.println(orderNumber);
			String jpql = "select c from CommodityItem c where c.orderNumber=:orderNumber";

			List<CommodityItem> items = em.createQuery(jpql)
					.setParameter("orderNumber", orderNumber).getResultList();

			return items;
		}

	
	
	
	
		@Override
		public boolean updateMailAndTel(Store store,String newemail) {
			/*em.refresh(admin);
			return true;*/
			int n = 0;

			String jpql = "update Store a set a.email=:name,a.passwd=:passwd,"
					+ "a.tele=:phone where a.email=:newemail";
			System.out.println("this is dao update:" + store.getEmail() + store.getPasswd() + newemail + store.getTele());
			n = em.createQuery(jpql).setParameter("name", newemail)
					.setParameter("passwd", store.getPasswd())
					.setParameter("phone", store.getTele())
					.setParameter("newemail", store.getEmail()).executeUpdate();

			return true;

		}

		@Override
		public int updatePasswd(String email, String newpassword) {
			int rs=0;
			jpql="update Store s set s.passwd=:newpassword where s.email=:email";
			//em.persist(s);
			rs = em.createQuery(jpql)
					.setParameter("newpassword", newpassword)
					.setParameter("email", email)
					.executeUpdate();
			return rs;
		}
	
		@Override
		public int deleteTouSuByOrderId(Integer orderId) {
			//寮�濮嬪垹闄よ鍗�
			System.out.println("鍒櫎瑷傚柈"+orderId);
			String jpql1="delete from OrderForm o where o.orderId=:orderId";
			int result1=em.createQuery(jpql1).setParameter("orderId", orderId).executeUpdate();
			//寮�濮嬪垹闄よ鍗曟潯鐩�
			System.out.println("鍒櫎姊濈洰");
			String jpql2="delete from CommodityItem c where c.orderId=:orderId";
			int result2=em.createQuery(jpql2).setParameter("orderId", orderId).executeUpdate();
			System.out.println("result1="+result1+"_____"+"result2="+result2);
			if(result1>0&&result2>0){
				return 1;
			}else{
				return 0;
			}
		}

	
	
	
	
	
	
	
	
	

	/*@Override
	public int post(Store s, HttpSession session) {
		Store store = (Store) session.getAttribute("store");
		String jpql="update Store s set s.sellerName=:sellerName,s.mobile=:mobile s.idCard=:idCard,s.obverseCardPhoto=:obverseCardPhoto,s.reverseCardPhoto=:reverseCardPhoto where s.email=:email";
		int rs = em.createQuery(jpql)
				.setParameter("sellerName",store.getSellerName())
				.setParameter("mobile", store.getMobile())
				.setParameter("idCard", store.getIdCard())
				.setParameter("obverseCardPhoto", store.getObverseCardPhoto())
				.setParameter("reverseCardPhoto", store.getReverseCardPhoto())
				.setParameter("email", store.getId())
				.executeUpdate();
		return rs;
	}*/

}
